package com.atldy.commerce.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.atldy.commerce.enums.CommonConstant;
import com.atldy.commerce.vo.JwtToken;
import com.atldy.commerce.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @version 1.0
 * @Author: li-dong-yang
 * @CreateTime: 2021-08-24 7:35
 * @Description： 使用ribbon进行HTTP服务间的通信
 */
@Service
@Slf4j
public class RibbonService {

    @Autowired
    private RestTemplate restTemplate;

    public JwtToken getRestJwtToken(UsernameAndPassword usernameAndPassword) {
        String url = StrUtil.format(
                "http://{}/ecommerce-authority-center/authority/token"
                , CommonConstant.AUTHORITY_CENTER_SERVICE_ID
        );
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return restTemplate.postForObject(url,
                new HttpEntity<>(JSON.toJSONString(usernameAndPassword), headers)
                , JwtToken.class);
    }



}